# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2011 OpenERP S.A (<http://www.openerp.com>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import hashlib
import itertools
import logging
import os
import re
from openerp import SUPERUSER_ID
import time
from openerp import tools
from openerp.osv import fields, osv

import base64
from urllib import urlencode
from urlparse import urljoin
from openerp.osv.orm import except_orm
from openerp.tools.translate import _

_logger = logging.getLogger(__name__)
from openerp.osv import fields, osv

class send_mail_user(osv.TransientModel):


    _name = "send.mail.user"
    _description = "Send Mail User Wizard"
    _columns = {
        'user_ids': fields.one2many('send.mail.user.line', 'wizard_id', string='Users'),
    }

    def default_get(self, cr, uid, fields, context=None):
        if context == None:
            context = {}
        user_ids = context.get('active_ids', [])
        wiz_id = context.get('active_id', None)
        res = []
        users = self.pool.get('res.users').browse(cr, uid, user_ids, context=context)
        for user in users:
            res.append((0, 0, {
                'wizard_id': wiz_id,
                'user_id': user.id,
                'user_login': user.login,
            }))
        return {'user_ids': res}
    
    def send(self, cr, uid, id, context=None):
        partner_obj = self.pool.get('res.partner')
        ir_mail_server = self.pool.get('ir.mail_server')
        wizard = self.browse(cr, uid, id, context=context)[0]
        users = [user.user_id for user in wizard.user_ids]
        print users
        if context is None:
            context = {}  
        post_values = {}
        for user in users:
            mess_string =''
            username = user.name
            mess_string += """<p>Username : %s .</p>
                                <p>Company : %s .</p>
                                <p>User Login : %s .</p>
                                <p>Password : %s .</p> """%(user.name, user.company_id.name, user.login, user.password )      
            
            active_model_pool_name = 'sale.order'
            active_model_pool = self.pool.get(active_model_pool_name)
            subtype = 'mail.mt_comment'    
            
            post_values = {
                'subject': 'Username: %s '%username,
                'body': mess_string,
                'parent_id': user.partner_id.id,
                'partner_ids': [user.partner_id.id],
#                 'partner_ids': [user.user_id.partner_id.id for user in wizard.user_ids],               
            }  
            ## gui mail           
            msg_id = active_model_pool.message_post(cr, SUPERUSER_ID, [0], type='comment', subtype=subtype, context=context, **post_values)    
            
        return {
            'type': 'ir.actions.act_window_close',
        }  



class send_mail_user_line(osv.TransientModel):

    _name = 'send.mail.user.line'
    _description = "Send Mail User Line Wizard"
    _columns = {
        'wizard_id': fields.many2one('send.mail.user', string='Wizard', required=True),
        'user_id': fields.many2one('res.users', string='User', required=True),
        'user_login': fields.char('User Login', readonly=True),
    }



